using System.Net;
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;

namespace 第九次课堂作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Crawler1 myCrawler = new Crawler1();
            string myUrl = textBox1.Text;
            myCrawler.myUrl = myUrl;
            myCrawler.Crawl();
            label2.Text = "电话号码：\n"+myCrawler.myPhoneNumber+ "\n"+ "邮箱：\n" + myCrawler.myEmail;
        }
    }
    public class Crawler1
    {
        public string myUrl = "";
        public string myPhoneNumber = "";
        public string myEmail = "";
        public string info1 = "空\n";
        public string info2 = "空\n";
        public string info3 = "空\n";

        public void Crawl()
        {
            string html = DownLoad(myUrl);
            Parse(html);
        }
        public string DownLoad(string url)
        {
            try
            {
                WebClient wc = new WebClient();
                wc.Encoding = Encoding.UTF8;
                string html = wc.DownloadString(url);
                string fileName = "page";
                File.WriteAllText(fileName, html, Encoding.UTF8);
                return html;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        public void Parse(string html)
        {
            string phoneNumber = "1[0-9]{10}";
            string email = @"[a-zA-Z0-9]+@[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+";
            MatchCollection matchs1 = new Regex(phoneNumber).Matches(html);
            MatchCollection matchs2 = new Regex(email).Matches(html);
            foreach (Match match1 in matchs1)
            {
                phoneNumber = match1.Value;
                myPhoneNumber += phoneNumber+" ";
            }
            foreach (Match match2 in matchs2)
            {
                email = match2.Value;
                myEmail += email + " ";
            }
        }

    }
}